package com.eascs.gaczcontract.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.item.service.O2oPubDdItemService;
import com.eascs.admin.o2o.internal.entity.PubDditem;
import com.eascs.admin.o2o.internal.service.LoanapplyExpandService;
import com.eascs.admin.o2o.internal.service.PubDditemService;
import com.eascs.admin.o2o.internal.util.MoneyUtil;
import com.eascs.admin.o2o.loan.entity.O2oLoanUserEntity;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanUserService;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.common.doc.word.WordPOIUntil;
import com.eascs.common.exception.RuntimeIOException;
import com.eascs.common.util.IoUtil;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.nsEditDetail.entity.O2oMainShopInfoEntity;
import com.eascs.crm.nsEditDetail.service.IMainShopInfoService;
import com.eascs.gaczcontract.entity.CustomerBasicInfo;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.workflow.common.service.sys.SysOrgService;

@Controller
public class JXContractRestContorller extends BaseRestController {
	@Autowired
	private O2oLoanapplyService loanService;
	@Autowired
	private O2oLoanUserService loanUserService;
	@Autowired
	LoanapplyExpandService loanapplyExpandService;
	@Autowired
	PubDditemService pubDditemService;
	@Autowired
	O2oPubDdItemService ddItemService;
	@Reference
	SysOrgService sysOrgService;
	@Reference
	IMainShopInfoService mainShopInfoService;
	@RequestMapping(value = "/toDownloadJX")
	public BaseRestResult<String> checkShopInfo(String loanapplyNo) {
		return BaseRestResult.info("正在下载中...").andRedirect("/downloadJXContract?loanapplyNo=" + loanapplyNo);
	}

	/*@RequestMapping(value = "/downloadJXContract", method = RequestMethod.GET)
	public void downloadJXContract(HttpServletResponse httpServletResponse, HttpServletRequest request,
			String loanapplyNo) {
		O2oLoanapplyEntity loanapply = loanService.findById(loanapplyNo);
		if (loanapply == null) {
			return;
		}
		// 最后需要生产的模板文件
		String[] classPathFiles = { "/pdfTemplate/jxbank/jxBankContract.pdf" };

		Map<String, Object> data = fillData(loanapplyNo);

		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		try {
			PdfBuilder.mergePdfFilesAndBuild(classPathFiles, data, outputStream);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		OutputStream os = null;
		try {
			String outfile = "";
			outfile = "江西银行带数据合同.pdf";
			if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
				outfile = URLEncoder.encode(outfile, "UTF-8");
			} else {
				outfile = new String(outfile.getBytes("UTF-8"), "ISO8859-1");
			}
			os = httpServletResponse.getOutputStream();
			httpServletResponse.reset();
			httpServletResponse.setContentType("application/pdf");
			httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
			os.write(outputStream.toByteArray());
			os.flush();
		} catch (IOException e) {
			throw new RuntimeIOException(e);
		} finally {
			IoUtil.close(os);
		}
	}

	private Map<String, Object> fillData(String loanapplyNo) {
		Map<String, Object> data = new HashMap<>();
		O2oLoanapplyEntity loanapply = loanService.findById(loanapplyNo);
		NumberFormat nf = new DecimalFormat(",###.00");
		if (loanapply == null) {
			return data;
		}
		O2oLoanUserEntity loanUserEntity = loanUserService.findById(loanapply.getLoanId(), loanapplyNo);
		CustomerBasicInfo customerBasicInfo = loanService.getCustomerEntityById(loanUserEntity.getId());
		O2oLoanUserEntity loanSpouseUserEntity = loanUserService.findSpouseId(loanUserEntity.getLoanId(), loanapplyNo);
		if (null != loanSpouseUserEntity && StringUtil.isNotBlank(loanSpouseUserEntity.getId())) {
			data.put("mateNameByGuara", loanSpouseUserEntity.getUserFullName());// 配偶(用于责任担保书-自然人版)
			data.put("mateIdCardByGuara", loanSpouseUserEntity.getDocumentNo());// 配偶身份证(用于责任担保书-自然人版)
		} else {
			data.put("mateNameByGuara", "");// 配偶(用于责任担保书-自然人版)
			data.put("mateIdCardByGuara", "");// 配偶身份证(用于责任担保书-自然人版)
		}

		O2oLoanUserEntity guarantorByGuara = loanUserService.queryDBById(loanapplyNo);
		if (guarantorByGuara != null) {
			data.put("guarByGuara", guarantorByGuara.getUserFullName());// 担保人(用于责任担保书-第三方担保人版)
			data.put("guarIdCardByGuara", guarantorByGuara.getDocumentNo());// 担保人身份证(用于责任担保书-第三方担保人版)
		} else {
			data.put("guarByGuara", "");// 担保人
			data.put("guarIdCardByGuara", "");// 担保人身份证
		}

		if (loanSpouseUserEntity != null) {
			data.put("mateName", loanSpouseUserEntity.getUserFullName());// 配偶
			data.put("mateIdCard", loanSpouseUserEntity.getDocumentNo());// 配偶身份证
			data.put("mateMobile", loanSpouseUserEntity.getUserPhone());// 配偶手机号
			CustomerBasicInfo mateBasicInfo = loanService.getCustomerEntityById(loanSpouseUserEntity.getId());
			if (mateBasicInfo != null) {
				String recipientAdress = "";
				if (StringUtil.isNotBlank(mateBasicInfo.getRecipientProvince())) {
					// @AUTO
					PubDditem p = pubDditemService.queryPubDditemCRM("9", mateBasicInfo.getRecipientProvince());
					if (p != null) {
						recipientAdress = recipientAdress + p.getItemName();
					}
				}

				if (StringUtil.isNotBlank(mateBasicInfo.getRecipientCity())) {
					PubDditem p = pubDditemService.queryPubDditemCRM("10", mateBasicInfo.getRecipientCity());
					if (p != null) {
						recipientAdress = recipientAdress + p.getItemName();
					}
				}

				if (StringUtil.isNotBlank(mateBasicInfo.getRecipientCounty())) {
					PubDditem p = pubDditemService.queryPubDditemCRM("11", mateBasicInfo.getRecipientCounty());
					if (p != null) {
						recipientAdress = recipientAdress + p.getItemName();
					}
				}

				if (mateBasicInfo.getRecipientAddress() != null) {
					recipientAdress = recipientAdress + mateBasicInfo.getRecipientAddress();
				}
				data.put("mateRecipientAdress", recipientAdress);// 收件地址

				String acountAdress = "";
				if (StringUtil.isNotBlank(mateBasicInfo.getAccountProvince())) {
					PubDditem p = pubDditemService.queryPubDditemCRM("9", mateBasicInfo.getAccountProvince());
					if (p != null) {
						acountAdress = acountAdress + p.getItemName();
					}
				}

				if (StringUtil.isNotBlank(mateBasicInfo.getAccountCity())) {
					PubDditem p = pubDditemService.queryPubDditemCRM("10", mateBasicInfo.getAccountCity());
					if (p != null) {
						acountAdress = acountAdress + p.getItemName();
					}
				}

				if (StringUtil.isNotBlank(mateBasicInfo.getAccountAounty())) {
					PubDditem p = pubDditemService.queryPubDditemCRM("11", mateBasicInfo.getAccountAounty());
					if (p != null) {
						acountAdress = acountAdress + p.getItemName();
					}
				}

				if (mateBasicInfo.getPermanentresidence() != null) {
					acountAdress = acountAdress + mateBasicInfo.getPermanentresidence();
				}
				data.put("mateAcountAdress", acountAdress);// 户口地址
			}
		} else {
			if (guarantorByGuara != null) {
				data.put("mateName", guarantorByGuara.getUserFullName());// 担保人
				data.put("mateIdCard", guarantorByGuara.getDocumentNo());// 担保人身份证
				data.put("mateMobile", guarantorByGuara.getUserPhone());// 担保人手机号
				CustomerBasicInfo guaraExp = loanService.getCustomerEntityById(guarantorByGuara.getId());
				String recipientAdress = "";
				String acountAdress = "";
				if (null != guaraExp) {
					if (StringUtil.isNotBlank(guaraExp.getRecipientProvince())) {
						PubDditem p = pubDditemService.queryPubDditemCRM("9", guaraExp.getRecipientProvince());
						if (p != null) {
							recipientAdress = recipientAdress + p.getItemName();
						}
					}

					if (StringUtil.isNotBlank(guaraExp.getRecipientCity())) {
						PubDditem p = pubDditemService.queryPubDditemCRM("10", guaraExp.getRecipientCity());
						if (p != null) {
							recipientAdress = recipientAdress + p.getItemName();
						}
					}

					if (StringUtil.isNotBlank(guaraExp.getRecipientCounty())) {
						PubDditem p = pubDditemService.queryPubDditemCRM("11", guaraExp.getRecipientCounty());
						if (p != null) {
							recipientAdress = recipientAdress + p.getItemName();
						}
					}

					if (guaraExp.getRecipientAddress() != null) {
						recipientAdress = recipientAdress + guaraExp.getRecipientAddress();
					}
					data.put("mateRecipientAdress", recipientAdress);// 收件地址

					if (StringUtil.isNotBlank(guaraExp.getAccountProvince())) {
						PubDditem p = pubDditemService.queryPubDditemCRM("9", guaraExp.getAccountProvince());
						if (p != null) {
							acountAdress = acountAdress + p.getItemName();
						}
					}

					if (StringUtil.isNotBlank(guaraExp.getAccountCity())) {
						PubDditem p = pubDditemService.queryPubDditemCRM("10", guaraExp.getAccountCity());
						if (p != null) {
							acountAdress = acountAdress + p.getItemName();
						}
					}

					if (StringUtil.isNotBlank(guaraExp.getAccountAounty())) {
						PubDditem p = pubDditemService.queryPubDditemCRM("11", guaraExp.getAccountAounty());
						if (p != null) {
							acountAdress = acountAdress + p.getItemName();
						}
					}

					if (guaraExp.getPermanentresidence() != null) {
						acountAdress = acountAdress + guaraExp.getPermanentresidence();
					}
				}
				data.put("mateAcountAdress", acountAdress);// 户口地址
			}
		}
		O2oMainShopInfoEntity shop = mainShopInfoService.getByLoanNo(loanapplyNo);

		if (loanUserEntity != null) {
			data.put("lonerName", loanUserEntity.getUserFullName());// 借款人
			data.put("idNumber", loanUserEntity.getDocumentNo());// 身份证号码
			data.put("mobile", loanUserEntity.getUserPhone());// 手机号
		}
		if (customerBasicInfo != null) {
			String recipientAdress = "";
			if (StringUtil.isNotBlank(customerBasicInfo.getRecipientProvince())) {
				PubDditem p = pubDditemService.queryPubDditemCRM("9", customerBasicInfo.getRecipientProvince());
				if (p != null) {
					recipientAdress = recipientAdress + p.getItemName();
				}
			}

			if (StringUtil.isNotBlank(customerBasicInfo.getRecipientCity())) {
				PubDditem p = pubDditemService.queryPubDditemCRM("10", customerBasicInfo.getRecipientCity());
				if (p != null) {
					recipientAdress = recipientAdress + p.getItemName();
				}
			}

			if (StringUtil.isNotBlank(customerBasicInfo.getRecipientCounty())) {
				PubDditem p = pubDditemService.queryPubDditemCRM("11", customerBasicInfo.getRecipientCounty());
				if (p != null) {
					recipientAdress = recipientAdress + p.getItemName();
				}
			}

			if (customerBasicInfo.getRecipientAddress() != null) {
				recipientAdress = recipientAdress + customerBasicInfo.getRecipientAddress();
			}
			data.put("recipientAdress", recipientAdress);// 收件地址

			String acountAdress = "";
			if (StringUtil.isNotBlank(customerBasicInfo.getAccountProvince())) {
				PubDditem p = pubDditemService.queryPubDditemCRM("9", customerBasicInfo.getAccountProvince());
				if (p != null) {
					acountAdress = acountAdress + p.getItemName();
				}
			}

			if (StringUtil.isNotBlank(customerBasicInfo.getAccountCity())) {
				PubDditem p = pubDditemService.queryPubDditemCRM("10", customerBasicInfo.getAccountCity());
				if (p != null) {
					acountAdress = acountAdress + p.getItemName();
				}
			}

			if (StringUtil.isNotBlank(customerBasicInfo.getAccountAounty())) {
				PubDditem p = pubDditemService.queryPubDditemCRM("11", customerBasicInfo.getAccountAounty());
				if (p != null) {
					acountAdress = acountAdress + p.getItemName();
				}
			}

			if (customerBasicInfo.getPermanentresidence() != null) {
				acountAdress = acountAdress + customerBasicInfo.getPermanentresidence();
			}
			data.put("acountAdress", acountAdress);// 户口地址
		}

		if (shop != null) {
			String mainShopAdd="";
			if (!StringUtils.isEmpty(shop.getShopprovince())) {
				String province=ddItemService.findPubdditemNameByCodeisall("province", shop.getShopprovince());
				if (!StringUtils.isEmpty(province)) {
					mainShopAdd+=province;
				}
			}
			if (!StringUtils.isEmpty(shop.getShopcity())) {
				String city=ddItemService.findPubdditemNameByCodeisall("city", shop.getShopcity());
				if (!StringUtils.isEmpty(city)) {
					mainShopAdd+=city;
				}
			}
			if (!StringUtils.isEmpty(shop.getShopcounty())) {
				String country=ddItemService.findPubdditemNameByCodeisall("country", shop.getShopcounty());
				if (!StringUtils.isEmpty(country)) {
					mainShopAdd+=country;
				}
			}
			String organizationForm="";
			if (!StringUtils.isEmpty(shop.getOrganizationForm())) {
				organizationForm=ddItemService.findPubdditemO2ONameByCodeall("organizationForm", shop.getOrganizationForm());
			}
			mainShopAdd+=shop.getShopaddress();
			data.put("licenceName", shop.getLicenceName());// 营业执照名字
			data.put("blrNumber", shop.getBlrNumber());// 营业执照注册号
			data.put("mainShopAdd", mainShopAdd);// 门店地址
			data.put("organizationForm", organizationForm);//组织形式
		}

		if (loanapply != null) {
			if (!"".equals(loanapply.getEaMount()) && loanapply.getEaMount() != null) {
				String bankAmountBigWrite = MoneyUtil
						.toChinese(formatComma2BigDecimal(loanapply.getEaMount()).toString());
				System.out.println(bankAmountBigWrite);

				data.put("bankAmountBigWrite", bankAmountBigWrite);// 银行批复金额大写

				data.put("bankAmountBigWrite-2", bankAmountBigWrite.substring(0, bankAmountBigWrite.length() - 2));// 银行复批金额大写去‘元整’

				data.put("loanAmount", "￥" + formatComma2BigDecimal(loanapply.getEaMount()));// 申请金额万元

				data.put("bankAmount", nf.format(formatComma2BigDecimal(loanapply.getEaMount())));// 银行批复金额小写

				data.put("bankAmount2", "￥" + nf.format(formatComma2BigDecimal(loanapply.getEaMount())));// 银行批复金额小写有￥号
			}

			data.put("loanTerm", loanapply.getLoanTerm());// 借款期限
		}
		return data;
	}*/
	
	
	
	
	/**
	 * @Title: downloadJXContract
	 * @Description: 下载江西银行合同
	 * @author 
	 * @param httpServletResponse
	 * @param request
	 * @param loanapplyNo
	 * @return void
	 * @throws
	 * @date 2018年1月24日 上午10:22:09
	 */
	@RequestMapping(value = "/downloadJXContract", method = RequestMethod.GET)
	public void downloadJXContract(HttpServletResponse httpServletResponse, HttpServletRequest request,
			String loanapplyNo) {
		O2oLoanapplyEntity loanapply = loanService.findById(loanapplyNo);
		if (loanapply == null) {
			return;
		}
		// 最后需要生产的模板文件
		String classPathFiles =  "/wordTemplate/jxBankContract.doc" ;

		Map<String, String> data = fillData(loanapplyNo);

		ByteArrayOutputStream outputStream=WordPOIUntil.downLoadDoc(classPathFiles, data);
		OutputStream os = null;
		try {
			String outfile = "";
			outfile = "江西银行带数据合同.doc";
			if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
				outfile = URLEncoder.encode(outfile, "UTF-8");
			} else {
				outfile = new String(outfile.getBytes("UTF-8"), "ISO8859-1");
			}
			os = httpServletResponse.getOutputStream();
			httpServletResponse.reset();
			httpServletResponse.setContentType("application/doc");
			httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
			os.write(outputStream.toByteArray());
			os.flush();
		} catch (IOException e) {
			throw new RuntimeIOException(e);
		} finally {
			IoUtil.close(os);
		}
	}

	private Map<String, String> fillData(String loanapplyNo) {
		Map<String, String> data = new HashMap<String, String>();
		O2oLoanapplyEntity loanapply = loanService.findById(loanapplyNo);
		NumberFormat nf = new DecimalFormat(",###.00");
		if (loanapply == null) {
			return data;
		}
		O2oLoanUserEntity loanUserEntity = loanUserService.findById(loanapply.getLoanId(), loanapplyNo);
		CustomerBasicInfo customerBasicInfo = loanService.getCustomerEntityById(loanUserEntity.getId());
		O2oLoanUserEntity loanSpouseUserEntity = loanUserService.findSpouseId(loanUserEntity.getLoanId(), loanapplyNo);
		if (null != loanSpouseUserEntity && StringUtil.isNotBlank(loanSpouseUserEntity.getId())) {
			data.put("${mateNameByGuara}", loanSpouseUserEntity.getUserFullName());// 配偶(用于责任担保书-自然人版)
			data.put("${mateIdCardByGuara}", loanSpouseUserEntity.getDocumentNo());// 配偶身份证(用于责任担保书-自然人版)
		} else {
			data.put("${mateNameByGuara}", "");// 配偶(用于责任担保书-自然人版)
			data.put("${mateIdCardByGuara}", "");// 配偶身份证(用于责任担保书-自然人版)
		}

		O2oLoanUserEntity guarantorByGuara = loanUserService.queryDBById(loanapplyNo);
		if (guarantorByGuara != null) {
			data.put("${guarByGuara}", guarantorByGuara.getUserFullName());// 担保人(用于责任担保书-第三方担保人版)
			data.put("${guarIdCardByGuara}", guarantorByGuara.getDocumentNo());// 担保人身份证(用于责任担保书-第三方担保人版)
		} else {
			data.put("${guarByGuara}", "");// 担保人
			data.put("${guarIdCardByGuara}", "");// 担保人身份证
		}

		if (loanSpouseUserEntity != null) {
			data.put("${mateName}", loanSpouseUserEntity.getUserFullName());// 配偶
			data.put("${mateIdCard}", loanSpouseUserEntity.getDocumentNo());// 配偶身份证
			data.put("${mateMobile}", loanSpouseUserEntity.getUserPhone());// 配偶手机号
			CustomerBasicInfo mateBasicInfo = loanService.getCustomerEntityById(loanSpouseUserEntity.getId());
			if (mateBasicInfo != null) {
				String recipientAdress = "";
				if (StringUtil.isNotBlank(mateBasicInfo.getRecipientProvince())) {
					// @AUTO
					PubDditem p = pubDditemService.queryPubDditemCRM("9", mateBasicInfo.getRecipientProvince());
					if (p != null) {
						recipientAdress = recipientAdress + p.getItemName();
					}
				}

				if (StringUtil.isNotBlank(mateBasicInfo.getRecipientCity())) {
					PubDditem p = pubDditemService.queryPubDditemCRM("10", mateBasicInfo.getRecipientCity());
					if (p != null) {
						recipientAdress = recipientAdress + p.getItemName();
					}
				}

				if (StringUtil.isNotBlank(mateBasicInfo.getRecipientCounty())) {
					PubDditem p = pubDditemService.queryPubDditemCRM("11", mateBasicInfo.getRecipientCounty());
					if (p != null) {
						recipientAdress = recipientAdress + p.getItemName();
					}
				}

				if (mateBasicInfo.getRecipientAddress() != null) {
					recipientAdress = recipientAdress + mateBasicInfo.getRecipientAddress();
				}
				data.put("${mateRecipientAdress}", recipientAdress);// 收件地址

				String acountAdress = "";
				if (StringUtil.isNotBlank(mateBasicInfo.getAccountProvince())) {
					PubDditem p = pubDditemService.queryPubDditemCRM("9", mateBasicInfo.getAccountProvince());
					if (p != null) {
						acountAdress = acountAdress + p.getItemName();
					}
				}

				if (StringUtil.isNotBlank(mateBasicInfo.getAccountCity())) {
					PubDditem p = pubDditemService.queryPubDditemCRM("10", mateBasicInfo.getAccountCity());
					if (p != null) {
						acountAdress = acountAdress + p.getItemName();
					}
				}

				if (StringUtil.isNotBlank(mateBasicInfo.getAccountAounty())) {
					PubDditem p = pubDditemService.queryPubDditemCRM("11", mateBasicInfo.getAccountAounty());
					if (p != null) {
						acountAdress = acountAdress + p.getItemName();
					}
				}

				if (mateBasicInfo.getPermanentresidence() != null) {
					acountAdress = acountAdress + mateBasicInfo.getPermanentresidence();
				}
				data.put("${mateAcountAdress}", acountAdress);// 户口地址
			}
		} else {
			if (guarantorByGuara != null) {
				data.put("${mateName}", guarantorByGuara.getUserFullName());// 担保人
				data.put("${mateIdCard}", guarantorByGuara.getDocumentNo());// 担保人身份证
				data.put("${mateMobile}", guarantorByGuara.getUserPhone());// 担保人手机号
				CustomerBasicInfo guaraExp = loanService.getCustomerEntityById(guarantorByGuara.getId());
				String recipientAdress = "";
				String acountAdress = "";
				if (null != guaraExp) {
					if (StringUtil.isNotBlank(guaraExp.getRecipientProvince())) {
						PubDditem p = pubDditemService.queryPubDditemCRM("9", guaraExp.getRecipientProvince());
						if (p != null) {
							recipientAdress = recipientAdress + p.getItemName();
						}
					}

					if (StringUtil.isNotBlank(guaraExp.getRecipientCity())) {
						PubDditem p = pubDditemService.queryPubDditemCRM("10", guaraExp.getRecipientCity());
						if (p != null) {
							recipientAdress = recipientAdress + p.getItemName();
						}
					}

					if (StringUtil.isNotBlank(guaraExp.getRecipientCounty())) {
						PubDditem p = pubDditemService.queryPubDditemCRM("11", guaraExp.getRecipientCounty());
						if (p != null) {
							recipientAdress = recipientAdress + p.getItemName();
						}
					}

					if (guaraExp.getRecipientAddress() != null) {
						recipientAdress = recipientAdress + guaraExp.getRecipientAddress();
					}
					data.put("${mateRecipientAdress}", recipientAdress);// 收件地址

					if (StringUtil.isNotBlank(guaraExp.getAccountProvince())) {
						PubDditem p = pubDditemService.queryPubDditemCRM("9", guaraExp.getAccountProvince());
						if (p != null) {
							acountAdress = acountAdress + p.getItemName();
						}
					}

					if (StringUtil.isNotBlank(guaraExp.getAccountCity())) {
						PubDditem p = pubDditemService.queryPubDditemCRM("10", guaraExp.getAccountCity());
						if (p != null) {
							acountAdress = acountAdress + p.getItemName();
						}
					}

					if (StringUtil.isNotBlank(guaraExp.getAccountAounty())) {
						PubDditem p = pubDditemService.queryPubDditemCRM("11", guaraExp.getAccountAounty());
						if (p != null) {
							acountAdress = acountAdress + p.getItemName();
						}
					}

					if (guaraExp.getPermanentresidence() != null) {
						acountAdress = acountAdress + guaraExp.getPermanentresidence();
					}
				}
				data.put("${mateAcountAdress}", acountAdress);// 户口地址
			}
		}
		O2oMainShopInfoEntity shop = mainShopInfoService.getByLoanNo(loanapplyNo);

		if (loanUserEntity != null) {
			data.put("${lonerName}", loanUserEntity.getUserFullName());// 借款人
			data.put("${idNumber}", loanUserEntity.getDocumentNo());// 身份证号码
			data.put("${mobile}", loanUserEntity.getUserPhone());// 手机号
		}
		if (customerBasicInfo != null) {
			String recipientAdress = "";
			if (StringUtil.isNotBlank(customerBasicInfo.getRecipientProvince())) {
				PubDditem p = pubDditemService.queryPubDditemCRM("9", customerBasicInfo.getRecipientProvince());
				if (p != null) {
					recipientAdress = recipientAdress + p.getItemName();
				}
			}

			if (StringUtil.isNotBlank(customerBasicInfo.getRecipientCity())) {
				PubDditem p = pubDditemService.queryPubDditemCRM("10", customerBasicInfo.getRecipientCity());
				if (p != null) {
					recipientAdress = recipientAdress + p.getItemName();
				}
			}

			if (StringUtil.isNotBlank(customerBasicInfo.getRecipientCounty())) {
				PubDditem p = pubDditemService.queryPubDditemCRM("11", customerBasicInfo.getRecipientCounty());
				if (p != null) {
					recipientAdress = recipientAdress + p.getItemName();
				}
			}

			if (customerBasicInfo.getRecipientAddress() != null) {
				recipientAdress = recipientAdress + customerBasicInfo.getRecipientAddress();
			}
			data.put("${recipientAdress}", recipientAdress);// 收件地址

			String acountAdress = "";
			if (StringUtil.isNotBlank(customerBasicInfo.getAccountProvince())) {
				PubDditem p = pubDditemService.queryPubDditemCRM("9", customerBasicInfo.getAccountProvince());
				if (p != null) {
					acountAdress = acountAdress + p.getItemName();
				}
			}

			if (StringUtil.isNotBlank(customerBasicInfo.getAccountCity())) {
				PubDditem p = pubDditemService.queryPubDditemCRM("10", customerBasicInfo.getAccountCity());
				if (p != null) {
					acountAdress = acountAdress + p.getItemName();
				}
			}

			if (StringUtil.isNotBlank(customerBasicInfo.getAccountAounty())) {
				PubDditem p = pubDditemService.queryPubDditemCRM("11", customerBasicInfo.getAccountAounty());
				if (p != null) {
					acountAdress = acountAdress + p.getItemName();
				}
			}

			if (customerBasicInfo.getPermanentresidence() != null) {
				acountAdress = acountAdress + customerBasicInfo.getPermanentresidence();
			}
			data.put("${acountAdress}", acountAdress);// 户口地址
		}

		if (shop != null) {
			String mainShopAdd="";
			if (!StringUtils.isEmpty(shop.getShopprovince())) {
				String province=ddItemService.findPubdditemNameByCodeisall("province", shop.getShopprovince());
				if (!StringUtils.isEmpty(province)) {
					mainShopAdd+=province;
				}
			}
			if (!StringUtils.isEmpty(shop.getShopcity())) {
				String city=ddItemService.findPubdditemNameByCodeisall("city", shop.getShopcity());
				if (!StringUtils.isEmpty(city)) {
					mainShopAdd+=city;
				}
			}
			if (!StringUtils.isEmpty(shop.getShopcounty())) {
				String country=ddItemService.findPubdditemNameByCodeisall("country", shop.getShopcounty());
				if (!StringUtils.isEmpty(country)) {
					mainShopAdd+=country;
				}
			}
			String organizationForm="";
			if (!StringUtils.isEmpty(shop.getOrganizationForm())) {
				organizationForm=ddItemService.findPubdditemO2ONameByCodeall("organizationForm", shop.getOrganizationForm());
			}
			mainShopAdd+=shop.getShopaddress();
			data.put("${licenceName}", shop.getLicenceName());// 营业执照名字
			data.put("${blrNumber}", shop.getBlrNumber());// 营业执照注册号
			data.put("${mainShopAdd}", mainShopAdd);// 门店地址
			data.put("${organizationForm}", organizationForm);//组织形式
		}else{
			data.put("${licenceName}", "");// 营业执照名字
			data.put("${blrNumber}", "");// 营业执照注册号
			data.put("${mainShopAdd}", "");// 门店地址
			
		}

		if (loanapply != null) {
			if (!"".equals(loanapply.getEaMount()) && loanapply.getEaMount() != null) {
				String bankAmountBigWrite = MoneyUtil
						.toChinese(formatComma2BigDecimal(loanapply.getEaMount()).toString());
				System.out.println(bankAmountBigWrite);

//				data.put("${bankAmountBigWrite}", bankAmountBigWrite);// 银行批复金额大写

//				data.put("${bankAmountBigWrite-2}", bankAmountBigWrite.substring(0, bankAmountBigWrite.length() - 2));// 银行复批金额大写去‘元整’
				data.put("${bankAmountBigWrite-2}", " ");
				
				data.put("${loanAmount}", "￥" + formatComma2BigDecimal(loanapply.getEaMount()));// 申请金额万元
				data.put("${bankAmount}", " ");
//				data.put("${bankAmount}", nf.format(formatComma2BigDecimal(loanapply.getEaMount())));// 银行批复金额小写

//				data.put("${bankAmount2}", "￥" + nf.format(formatComma2BigDecimal(loanapply.getEaMount())));// 银行批复金额小写有￥号
			}
			
			if (loanapply.getRisk() != null) {
				NumberFormat nfd = new DecimalFormat("#0.0000");// 小数点保留四位，零不省略
				BigDecimal saferate = loanapply.getRisk().multiply(new BigDecimal(100));// 乘以100
//				data.put("saferate", formatComma2BigDecimal(saferate));// 风险补偿金费率
				data.put("${saferate}", nfd.format(saferate));// 风险补偿金费率
			}
			data.put("${billCode1}", "江西银行-合-" + loanapply.getBillCode());// 协议编号
			data.put("${loanTerm}", String.valueOf(loanapply.getLoanTerm()));// 借款期限
		}
		return data;
	}

	private boolean isDouble(String value) {
		try {
			Double.parseDouble(value);
			if (value.contains("."))
				return true;
			return false;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	private boolean isInteger(String value) {
		try {
			Integer.parseInt(value);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	private boolean isNumber(String value) {
		return isInteger(value) || isDouble(value);
	}

	private BigDecimal formatComma2BigDecimal(Object obj) {
		String val = String.valueOf(obj);
		if (val == null)
			return new BigDecimal("0.00");

		val = val.replaceAll(",", "");
		if (!isNumber(val))
			return new BigDecimal("0.00");

		BigDecimal decimal = new BigDecimal(val);
		decimal = decimal.setScale(2, BigDecimal.ROUND_UP);

		return decimal;

	}
	
	
}
